﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.OtbData.Register
{
    public class PresenterRegisterOtbData
    {
        IRegisterBasicOtbDataView viewRegisterBasic;
        IRegisterPrincipalView view;
        Model.OtbData otbData = new Model.OtbData() { Person = new Model.Person() };

        public PresenterRegisterOtbData(IRegisterPrincipalView view)
        {
            this.view = view;
        }

        public void GoStepTwo()
        {
            view.RegisterBasic.ChargeDataToBasic();
            otbData = view.RegisterBasic.OtbDataToRegister;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void RegisterOtbData()
        {
            view.RegisterBasic.ChargeDataToBasic();
            otbData = view.RegisterBasic.OtbDataToRegister;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                view.RefreshOtbDataMainView(otbData.Background, otbData.Name);
                ManagerOtbData.RegisterOtbData(otbData);
                view.SystemMessage("Los datos Fueron Registrados Exitosamente.!!!!");
                view.RestartRegister();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.OtbData mp = new Model.OtbData();
                mp.Name = view.RegisterBasic.OtbDataToRegister.Name;
                mp.Managment = view.RegisterBasic.OtbDataToRegister.Managment;
                mp.ExpirationDay = view.RegisterBasic.OtbDataToRegister.ExpirationDay;
                mp.Background = view.RegisterBasic.OtbDataToRegister.Background;
                ManagerOtbData.VerifyOtbDataIsCorrect(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }
        
        private bool PassSecondStep()
        {

            try
            {
                Model.OtbData mp = new Model.OtbData() { Person = new Model.Person() };
                mp.Person.PersonId = view.RegisterBasic.OtbDataToRegister.Person.PersonId;
                mp.Person.Names = view.RegisterBasic.OtbDataToRegister.Person.Names;
                mp.Person.LastNames = view.RegisterBasic.OtbDataToRegister.Person.LastNames;
                mp.Person.Phone = view.RegisterBasic.OtbDataToRegister.Person.Phone;

                ManagerOtbData.VerifyDataPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        public void GoBackPage()
        {
            viewRegisterBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewRegisterBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.RegisterBasic.ChargeDataToBasic();
            otbData = view.RegisterBasic.OtbDataToRegister;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {

            if (viewRegisterBasic != null)
                otbData = viewRegisterBasic.OtbDataToRegister;
            viewRegisterBasic = view.RegisterBasic;
            viewRegisterBasic.OtbDataToRegister = otbData;
            viewRegisterBasic.UpdateViewData();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
